package com.hjqjl.whdemo.feature.feature0.adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.hjqjl.whdemo.databinding.ItemFruitlistBinding;
import com.hjqjl.whdemo.feature.feature0.ui.activity.list.recyclerview.model.FruitModel;

import org.jetbrains.annotations.NotNull;

import java.util.List;

public class FruitAdapter extends RecyclerView.Adapter<FruitAdapter.ViewHolder> {
    private List<FruitModel> mFruitModelList;

    static class ViewHolder extends RecyclerView.ViewHolder {
        ImageView homeIvFruitListItemIc;
        TextView homeTvFruitListItemName;

        public ViewHolder(ItemFruitlistBinding binding) {
            super(binding.getRoot());
            homeIvFruitListItemIc = binding.homeIvFruitListItemIc;
            homeTvFruitListItemName = binding.tvFruitListItemName;
        }
    }

    public FruitAdapter(List<FruitModel> fruitModelList) {
        mFruitModelList = fruitModelList;
    }

    @NotNull
    @Override
    public ViewHolder onCreateViewHolder(@NotNull ViewGroup parent, int viewType) {
        ItemFruitlistBinding binding = ItemFruitlistBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        ViewHolder holder = new ViewHolder(binding);
        holder.itemView.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            FruitModel fruitModel = mFruitModelList.get(position);
            ToastUtils.showShort("position:" + position + "==我是itemView==" + fruitModel.getName());
        });
        holder.homeIvFruitListItemIc.setOnClickListener(v -> {
            int position = holder.getAdapterPosition();
            LogUtils.v("" + position);
            FruitModel fruitModel = mFruitModelList.get(position);
            ToastUtils.showShort("position:" + position + "==我是图片控件homeIvFruitListItemIc=="
                    + fruitModel.getName());
        });
        return holder;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        FruitModel fruitModel = mFruitModelList.get(position);
        holder.homeIvFruitListItemIc.setImageResource(fruitModel.getImageId());
        holder.homeTvFruitListItemName.setText(fruitModel.getName());
    }

    @Override
    public int getItemCount() {
        return mFruitModelList.size();
    }
}